<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\RoleService;
use Illuminate\Http\Request;
use  App\V3\V3_1\Models\RoleModel;
use Validator;

class RoleController extends BaseController
{
    /**
     * @新建角色信息
     * created 在 2021/11/20 15:13 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'role_name' =>'required',//角色名
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $checkRoleInfo = RoleModel::where('role_name', $data['role_name'])->first();

        if(!empty($checkRoleInfo)) {
            return $this->fail('', '角色已存在');
        }

        $insertId = RoleModel::insertGetId($data);

        $this->log('新建角色', $data);
        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => ['id' => $insertId]
        ]);
    }

    /**
     * @新建角色信息
     * created 在 2021/11/20 15:26 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//角色名
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        RoleModel::where('id', $data['id'])->update(['delete_flag' => 1]);
        $this->log('删除角色', $data);
        return $this->success();
    }

    /**
     * @修改角色权限信息
     * created 在 2021/11/20 15:35 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //type:修改权限信息和设置角色权限;0:修改角色;1:设置角色权限;
        if($request->type) {
            $this->log('设置角色权限', $request->all());
            $result = RoleService::roleSetting($request->all());
        }
        else {
            $this->log('修改角色', $request->all());
            $result = RoleService::modifyRole($request->all());
        }

        return $result;
    }

    /**
     * @角色列表
     * created 在 2021/11/20 15:53 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return response()->json([
            'code' => 0,
            'msg' => 'success',
            'data' => RoleModel::select(
                        'id',
                        'role_name'
                    )
                    ->where('delete_flag', 0)
                    ->orderBy('id', 'desc')
                    ->paginate($data['pageSize'],['*'], '',$data['currentPage'])
        ]);
    }

    /**
     * @角色权限详细
     * created 在 2021/11/20 15:57 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        if($this->getUserId() != 1) {
            return $this->fail('', '权限不足');
        }

        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' =>'required|integer',//角色id
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $result = RoleService::detail($data['id']);
        return $this->success($result);
    }
}
